package com.ztc.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

import org.apache.commons.lang.StringUtils;

public class NumberFormatUtil {
	
	private DecimalFormat df = null;
	
	public NumberFormatUtil(String formatStr) {
		df = new DecimalFormat(formatStr);
	}
	
	
	/**
	 * 向最接近数字方向舍入的舍入模式，四舍五入HALF_UP
	 * @param obj
	 * @return
	 */
	public String format(Object obj) {
		if(StringUtils.isBlank(obj + "")) {
			return "";
		}
		String str = obj + "";
		//不能转化为数字，直接返回字符串
		try {
			df.setRoundingMode(RoundingMode.HALF_UP);
			str = df.format(new BigDecimal((obj + "").replace(",", "")));
		} catch (Exception e) {
			
		}
		
		return str;
	}
}
